package com.sgb.kjwl.view.widget;

import android.content.Context;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.enterprise.IpPickSelectEntity;
import com.sgb.kjwl.utils.PickPriceUtils;
import com.sgb.kjwl.view.ui.adapter.enterprise.IpPickSelectAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;

import java.util.ArrayList;
import java.util.List;

public class MakeOverMoreFilterPopWindow extends BasePopWindow {


    private RecyclerView rv_price;
    private RecyclerView rv_publish_type;

    private TextView tv_reset;
    private TextView tv_sure;
    private TextView tv_price;
    private TextView tv_publish_type;
    private View viewBg;

    private IpPickSelectEntity mEntity;
    private IpPickSelectEntity mEntityResult;
    private RelativeLayout rl_publishtype;
    int priceDefItem;
    int publishTypeDefItem;
    int priceDefItemResult;
    int publishTypeDefItemResult;

    IpPickSelectAdapter priceAdapter;
    IpPickSelectAdapter publishTypeAdapter;

    private List<IpPickSelectEntity> priceList;//转让价格
    private List<IpPickSelectEntity> publishTypeList;//发布类型
    private ImageView image_z_frist;


    public MakeOverMoreFilterPopWindow(Context context) {
        super(context, DIR_RIGHT_MATCH);
        setSoftInputMode
                (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN |
                        WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_filter_make_over_more;
    }

    @Override
    protected void initView(PopViewHolder holder) {

        mEntity = new IpPickSelectEntity();
        image_z_frist = holder.getPop().findViewById(R.id.image_z_frist);

        rv_price = holder.getPop().findViewById(R.id.rv_fragment_intel_prop_brand);
        rv_publish_type = holder.getPop().findViewById(R.id.rv_fragment_intel_prop_brand_publish);
        rl_publishtype = holder.getPop().findViewById(R.id.rl_publishtype);
        tv_price = holder.getPop().findViewById(R.id.tv_fragment_intel_prop_brand_select);
        tv_publish_type = holder.getPop().findViewById(R.id.tv_fragment_intel_prop_brand_publish_select);
        tv_reset = holder.getPop().findViewById(R.id.reset_step);
        tv_sure = holder.getPop().findViewById(R.id.sure_step);
        viewBg = holder.getPop().findViewById(R.id.gray_layout);
        viewBg.setOnClickListener(view -> dissMiss());
        image_z_frist.setOnClickListener((View v) -> {
            priceAdapter.setDatas(priceAdapter.getDatas().size() > 9 ? priceList.subList(0, 9) : priceList);
            image_z_frist.setImageResource(priceAdapter.getDatas().size() > 9 ? R.mipmap.btn_expand_up_gray : R.mipmap.btn_expand1);
        });

        initResetView();
        initSureView();
        initAdapter();
        setPublishTypeData();
    }

    /**
     * 初始化adapter
     */
    private void initAdapter() {
        priceAdapter = new IpPickSelectAdapter(mContext, null);
        priceAdapter.setItemSelect(true);
        priceAdapter.setDefItem(0);
        rv_price.setLayoutManager(new GridLayoutManager(mContext, 3));
        rv_price.setAdapter(priceAdapter);
        tv_price.setText("");
        mEntity.setName("不限");

        priceAdapter.setOnItemClick(new AdapterOnItemClick<IpPickSelectEntity>() {
            @Override
            public void onItemClick(IpPickSelectEntity ipPickSelectEntity, int position) {
                if (onResultCallBack != null) {
                    mEntity.setMaxPrice(ipPickSelectEntity.getMaxPrice());
                    mEntity.setMinPrice(ipPickSelectEntity.getMinPrice());
                    mEntity.setName(ipPickSelectEntity.getName());
                    tv_price.setText(ipPickSelectEntity.getName().equals("不限") ? "" : ipPickSelectEntity.getName());
                    priceDefItem = position;
                }
            }
        });

        publishTypeAdapter = new IpPickSelectAdapter(mContext, null);
        publishTypeAdapter.setItemSelect(true);
        publishTypeAdapter.setDefItem(0);
        rv_publish_type.setLayoutManager(new GridLayoutManager(mContext, 3));
        rv_publish_type.setAdapter(publishTypeAdapter);
        tv_publish_type.setText("");

        publishTypeAdapter.setOnItemClick(new AdapterOnItemClick<IpPickSelectEntity>() {
            @Override
            public void onItemClick(IpPickSelectEntity ipPickSelectEntity, int position) {
                if (onResultCallBack != null) {
                    mEntity.setPublishType(ipPickSelectEntity.getPublishType());
                    tv_publish_type.setText(ipPickSelectEntity.getName().equals("不限") ? "" : ipPickSelectEntity.getName());
                    mEntity.setPublishTypeName(ipPickSelectEntity.getName());
                    publishTypeDefItem = position;
                }
            }
        });
    }

    private void setPublishTypeData() {
        publishTypeList = new ArrayList<>();
        IpPickSelectEntity entity = new IpPickSelectEntity();
        entity.setName("不限");
        entity.setPublishType("");
        publishTypeList.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("团队");
        entity.setPublishType("2");
        publishTypeList.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("个人");
        entity.setPublishType("0");
        publishTypeList.add(entity);

        entity = new IpPickSelectEntity();
        entity.setName("企业");
        entity.setPublishType("1");
        publishTypeList.add(entity);

        publishTypeAdapter.setDatas(publishTypeList);
    }

    public void resetWindow() {
        if (mEntityResult != null) {
            tv_price.setText(mEntityResult.getName() == null || mEntityResult.getName().equals("不限") ? "" : mEntityResult.getName());
            tv_publish_type.setText(mEntityResult.getPublishTypeName() == null || mEntityResult.getPublishTypeName().equals("不限") ? "" : mEntityResult.getPublishTypeName());
            priceAdapter.setDefItem(priceDefItemResult);
            publishTypeAdapter.setDefItem(publishTypeDefItemResult);
        } else {
            tv_price.setText("");
            tv_publish_type.setText("");
            priceAdapter.setDefItem(0);
            publishTypeAdapter.setDefItem(0);
        }
    }


    private void initResetView() {
        //重置
        tv_reset.setOnClickListener(view -> {
            mEntityResult = null;
            priceAdapter.setDefItem(0);
            publishTypeAdapter.setDefItem(0);
            mEntity.setPublishType("");
            mEntity.setMinPrice("");
            mEntity.setMaxPrice("");
            mEntity.setName("不限");
            mEntity.setPublishTypeName("不限");
            tv_price.setText("");
            tv_publish_type.setText("");
            priceDefItem = 0;
            publishTypeDefItem = 0;
//            onResultCallBack.result(null);
        });
    }


    private void initSureView() {
        //确定
        tv_sure.setOnClickListener(view -> {
            if (mEntityResult == null)
                mEntityResult = new IpPickSelectEntity();
            mEntityResult.setName(mEntity.getName() == null ? "" : mEntity.getName());
            mEntityResult.setPublishTypeName(mEntity.getPublishTypeName() == null ? "" : mEntity.getPublishTypeName());

            priceDefItemResult = priceDefItem;
            publishTypeDefItemResult = publishTypeDefItem;
            onResultCallBack.result(mEntity);
            dissMiss();
        });
    }

    public void setPriceData(List<IpPickSelectEntity> list) {
        List<IpPickSelectEntity> listChange = new ArrayList<>();
        listChange = PickPriceUtils.showPriceList(list);
        priceList = new ArrayList<>();
        priceList = listChange;
        priceAdapter.setDatas(listChange);
        if (listChange.size() > 9) {
            image_z_frist.setVisibility(View.VISIBLE);
            image_z_frist.setImageResource(R.mipmap.btn_expand1);
            priceAdapter.setDatas(priceList.subList(0, 9));
        } else {
            image_z_frist.setVisibility(View.GONE);
        }
    }

    /**
     * 设置是否显示选择发布方式
     * @param viewtype
     */
    public void setPublishTypeVisibility(int viewtype){
        rl_publishtype.setVisibility(viewtype);
    }

}
